﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlueLotus7.Eden.KB.Models;
using BlueLotus7.Eden.Service;
using BlueLotus7.ArchiveSystem.Common;
using BlueLotus7.Web;
using BlueLotus7.Eden.Management.DomainScope.Models;
using BlueLotus7.Eden.Web.Models;

namespace BlueLotus7.Eden.Web.Areas.Management.Controllers
{
    public class DomainScopeController : BaseController
    {
        //
        // GET: /Management/DomainScope/

        #region Pages

        /// <summary>
        /// Query domain scope page
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            DomainScopeQueryPageModel model = new DomainScopeQueryPageModel();
            return View("Query", model);
        }

        /// <summary>
        /// Users page
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public ActionResult Users(Guid key)
        {
            DomainScopeUsersPageModel model = null;

            try
            {
                ArchiveSystemCommonService service = new ArchiveSystemCommonService(true);
                DomainScope domainScope = service.GetDomainScopeByKey(key);

                if (domainScope != null)
                {
                    model = new DomainScopeUsersPageModel();
                    model.DomainScopeUsers.AddRange(service.QueryDomainScopeUsers(key, null));
                    model.DomainScopeName = domainScope.Name;
                }
            }
            catch (Exception ex)
            {
                Global.Logger.LogException(ex, key);
            }

            return View("Users", model);
        }

        /// <summary>
        /// View scope page
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public ActionResult ViewScope(Guid key)
        {
            DomainScope model = null;

            try
            {
                ArchiveSystemCommonService service = new ArchiveSystemCommonService(true);
                model = service.GetDomainScopeByKey(key);
                ViewBag.Flags = new ViewFlags();
                ViewBag.Flags[ViewFlags.CanEditFlag] = true;
                ViewBag.Flags[ViewFlags.ShowOKButtonFlag] = false;
            }
            catch (Exception ex)
            {
                Global.Logger.LogException(ex, key);
            }

            return View("ViewScope", model);
        }

        public ActionResult ViewUser(Guid key)
        {
            DomainScopeUser model = null;

            try
            {
                ArchiveSystemCommonService service = new ArchiveSystemCommonService(true);
                model = service.GetDomainScopeUserByKey(key);
                ViewBag.Flags = new ViewFlags();
                ViewBag.Flags[ViewFlags.CanEditFlag] = true;
                ViewBag.Flags[ViewFlags.ShowOKButtonFlag] = false;
            }
            catch (Exception ex)
            {
                Global.Logger.LogException(ex, key);
            }

            return View("ViewScopeUser", model);
        }

        public ActionResult IMScopeUsers(Guid key)
        {
            IMScopeUsersPageModel model = null;

            try
            {
                ArchiveStatusService service = new ArchiveStatusService(true);
                DomainScope scope = service.GetDomainScopeByKey(key);
                if (scope != null)
                {
                    model = new IMScopeUsersPageModel();
                    model.DomainScopeKey = key;
                    model.DomainScopeName = scope.Name;
                    model.EnumType = Helper.GetStatusEnumTypeByCode(scope.Code);
                    model.DomainScopeUsers.AddRange(service.GetDomainScopeUsersWithStatus(key));
                }
            }
            catch (Exception ex)
            {
                Global.Logger.LogException(ex, key);
            }

            return View("IMScopeUsers", model);
        }

        #endregion

        #region Ajax Handlers

        public JsonResult AddOrUpdateDomainScope(DomainScope domainScope)
        {
            AjaxResult result = new AjaxResult();

            try
            {
                ArchiveSystemCommonService service = new ArchiveSystemCommonService(true);
                DomainScope originalDomainScope = service.GetDomainScopeByCode(domainScope.Code);

                if (originalDomainScope != null && originalDomainScope.Key != domainScope.Key && !Validator.IsZeroGuid(domainScope.Key))
                {
                    //conflict
                    throw new Exception(LogMessageHelper.GetMessage(LogMessageHelper.OperationFailed_DataConflict));
                }
                else
                {
                    Guid? newGuid = service.AddOrUpdateDomainScope(domainScope);
                    if (newGuid != null)
                    {
                        result.IsSuccess = true;
                        result.Data = newGuid.Value.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Global.Logger.LogException(ex, domainScope);
            }

            return Json(result);
        }

        public JsonResult UpdateIMNeedMonitor(UpdateIMUsersRequest request)
        {
            AjaxResult result = new AjaxResult();
            if (request != null && request.Items != null && request.Items.Count > 0)
            {
                try
                {
                    ArchiveSystemCommonService service = new ArchiveSystemCommonService(true);

                    foreach (var one in request.Items)
                    {
                        DomainScopeUser user = service.GetDomainScopeUserByKey(one.Key);
                        if (user != null)
                        {
                            user.MonitorStatus = one.NeedMonitor;
                            service.AddOrUpdateScopeUser(user);
                        }
                    }
                    result.IsSuccess = true;
                }
                catch (Exception ex)
                {
                    Global.Logger.LogException(ex, request);
                }
            }
            return Json(result);
        }

        public PartialViewResult QueryDomainScope(QueryDomainScopeRequest request)
        {
            DomainScopeGuidControlModel model = new DomainScopeGuidControlModel();
            try
            {
                ArchiveSystemCommonService service = new ArchiveSystemCommonService(true);
                model.DomainScopes.AddRange(service.QueryDomainScopeList(request.Type, request.Keyword));
                model.ShowIMLink = true;
            }
            catch (Exception ex)
            {
                Global.Logger.LogException(ex, request);
            }

            return PartialView("ControlView/DomainScopeGridControlView", model);
        }

        public PartialViewResult GetDomainScopeInfo(Guid key)
        {
            DomainScope model = null;

            try
            {
                ArchiveSystemCommonService service = new ArchiveSystemCommonService(true);
                model = service.GetDomainScopeByKey(key);
                var flags = new Dictionary<string, bool>();
                ViewBag.Flags = new ViewFlags();
                ViewBag.Flags[ViewFlags.CanEditFlag] = true;
                ViewBag.Flags[ViewFlags.ShowOKButtonFlag] = true;
            }
            catch (Exception ex)
            {
                Global.Logger.LogException(ex, key);
            }

            return PartialView("ControlView/DomainScopeInfoControlView", model);
        }




        #endregion
    }
}
